package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntitySchoolWifiInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.schoolwifiinfo.SchoolWifiInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoSchoolWifiInfo;
import com.ocom.payconfig.service.SchoolWifiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
@Slf4j
public class SchoolWifiServiceImpl extends ServiceImpl<IDaoSchoolWifiInfo, EntitySchoolWifiInfo> implements SchoolWifiService {
    @Autowired
    private IDaoSchoolWifiInfo iDaoSchoolWifiInfo;

    @Autowired
    private SchoolWifiService schoolWifiService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Result selectSchoolWifi(Long comId, SchoolWifiInfoVo schoolWifiInfoVo) {
        if (ObjectUtil.isEmpty(schoolWifiInfoVo.getPage())) {
            schoolWifiInfoVo.setPage(1);
        }
        if (ObjectUtil.isEmpty(schoolWifiInfoVo.getSize())) {
            schoolWifiInfoVo.setSize(10);
        }

        PageHelper.startPage(schoolWifiInfoVo.getPage(), schoolWifiInfoVo.getSize());

        List<SchoolWifiInfoVo> list = iDaoSchoolWifiInfo.getSchoolWifiList(schoolWifiInfoVo);
        PageInfo<SchoolWifiInfoVo> pageInfo = new PageInfo<>(list);

        if (ObjectUtil.isEmpty(pageInfo.getList())) {
            return ResultUtil.success();
        }

        if (ObjectUtil.isEmpty(schoolWifiInfoVo.getPId())) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("total", pageInfo.getTotal());
            map.put("list", pageInfo.getList());
            return ResultUtil.success(map);
        }

        SchoolWifiInfoVo wifiInfoVo = pageInfo.getList().get(0);
         return ResultUtil.success(wifiInfoVo);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster =MasterSlaveEnum.MASTER )
    public Result modifySchoolWifi(Long comId, SchoolWifiInfoVo schoolWifiInfoVo) {

        LambdaUpdateWrapper<EntitySchoolWifiInfo> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(EntitySchoolWifiInfo::getComId, schoolWifiInfoVo.getComId())
                .eq(EntitySchoolWifiInfo::getPId, schoolWifiInfoVo.getPId());

        updateWrapper.set(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getLoginName()), EntitySchoolWifiInfo::getLoginName, schoolWifiInfoVo.getLoginName())
                .set(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getUserName()), EntitySchoolWifiInfo::getUserName, schoolWifiInfoVo.getUserName())
                .set(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getPassword()), EntitySchoolWifiInfo::getPassword, schoolWifiInfoVo.getPassword())
                .set(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getRemark()), EntitySchoolWifiInfo::getRemark, schoolWifiInfoVo.getRemark());

        if (ObjectUtil.isNotEmpty(schoolWifiInfoVo.getStatus())&&(schoolWifiInfoVo.getStatus()==1||schoolWifiInfoVo.getStatus()==2)) {
            updateWrapper.set(EntitySchoolWifiInfo::getStatus, schoolWifiInfoVo.getStatus())
                    .set(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getUpdateUser()), EntitySchoolWifiInfo::getCheckUser, schoolWifiInfoVo.getUpdateUser())
                    .set(EntitySchoolWifiInfo::getCheckTime, new Date());
        } else {
            updateWrapper.set(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getUpdateUser()), EntitySchoolWifiInfo::getUpdateUser, schoolWifiInfoVo.getUpdateUser())
                    .set(EntitySchoolWifiInfo::getUpdateTime, new Date());
        }
        if(ObjectUtil.isNotEmpty(schoolWifiInfoVo.getStatus())&&schoolWifiInfoVo.getStatus()==0){
            updateWrapper.set(EntitySchoolWifiInfo::getStatus, schoolWifiInfoVo.getStatus())
                    .set(EntitySchoolWifiInfo::getUpdateTime,new Date());
        }

        boolean flag = schoolWifiService.update(updateWrapper);

        if (!flag) {
            EntitySchoolWifiInfo entitySchoolWifiInfo = new EntitySchoolWifiInfo();
            entitySchoolWifiInfo.setComId(schoolWifiInfoVo.getComId());
            entitySchoolWifiInfo.setPId(schoolWifiInfoVo.getPId());
            entitySchoolWifiInfo.setLoginName(schoolWifiInfoVo.getLoginName());
            entitySchoolWifiInfo.setUserName(schoolWifiInfoVo.getUserName());
            entitySchoolWifiInfo.setPassword(schoolWifiInfoVo.getPassword());
            entitySchoolWifiInfo.setRemark(schoolWifiInfoVo.getRemark());
            entitySchoolWifiInfo.setStatus((byte) (schoolWifiInfoVo.getStatus()==null?0:schoolWifiInfoVo.getStatus()));
            entitySchoolWifiInfo.setUpdateUser(schoolWifiInfoVo.getUpdateUser());
            entitySchoolWifiInfo.setUpdateTime(new Date());
            try {
                schoolWifiService.save(entitySchoolWifiInfo);
            } catch (Exception e) {
                log.info("异常信息{}",e.getMessage());
                return ResultUtil.error(ResultEnum.Err_2016);
            }
        }
        return ResultUtil.success();
    }
}
